﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ZuesControls.Helpers;
using ZuesControls.Models;

namespace ZuesControls.Converters
{
    public class LineInfoMultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length != 2) return Enumerable.Empty<LineInfo>();
            if (values[0] is not int level || level <= 0) return Enumerable.Empty<LineInfo>();
            if (values[1] is not TreeViewItem currentItem) return Enumerable.Empty<LineInfo>();
            List<LineInfo> lines = new();
            TreeViewItem parentItem = VisualHelper.TryFindParent<TreeViewItem>(currentItem);
            TreeViewItem grandParentItem = VisualHelper.TryFindParent<TreeViewItem>(parentItem);
            bool parentIsLast = false;
            if (grandParentItem is TreeViewItem gParent)
            {
                var grandedata = grandParentItem.DataContext as ITreeNode;
                int dex = grandedata.Children.IndexOf(parentItem.DataContext as ITreeNode);
                if (dex == (grandedata.Children.Count - 1))
                {
                    parentIsLast = true;
                }
            }
            for (int i = 1; i <= level; i++)
            {
                lines.Add(new LineInfo { Level = i });
            }
            if (parentIsLast)
            {
                return Enumerable.Empty<LineInfo>();
            }
            return lines;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
